package com.cloud.basic.connectwifi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class WifiConnector {

    /**
     * 连接wifi
     * @param ssid
     * @param password
     */
    public static void connectToWifi(String ssid, String password) {
        String os = System.getProperty("os.name").toLowerCase();
        String command = "";
        Charset charset = Charset.defaultCharset();

        if (os.contains("win")) {
            command = "netsh wlan connect name=\"" + ssid + "\"";
            charset = Charset.forName("GBK"); // Windows默认字符编码
        } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
            command = "nmcli dev wifi connect " + ssid + " password " + password;
        } else {
            System.err.println("Unsupported OS");
            return;
        }

        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), charset));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 断开当前wifi
     */
    public static void disconnectWifi() {
        String os = System.getProperty("os.name").toLowerCase();
        String command = "";
        Charset charset = Charset.defaultCharset();

        if (os.contains("win")) {
            command = "netsh wlan disconnect";
            charset = Charset.forName("GBK"); // Windows默认字符编码
        } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
            command = "nmcli dev disconnect wlan0"; // 假设WiFi接口为wlan0
        } else {
            System.err.println("Unsupported OS");
            return;
        }

        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), charset));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void getWifiConnectStatus() {
        String os = System.getProperty("os.name").toLowerCase();
        String command = "";
        Charset charset = Charset.defaultCharset();

        if (os.contains("win")) {
            command = "netsh wlan show interfaces";
            charset = Charset.forName("GBK"); // Windows默认字符编码
        } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
            command = "nmcli dev status";
        } else {
            System.err.println("Unsupported OS");
            return;
        }

        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), charset));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        getWifiConnectStatus();
        disconnectWifi();
        // Replace with your WiFi network and password
        connectToWifi("Cloud_MF_Wi-Fi5", "coiot_mes#147");
    }
}